<template>
    <div class="app-container">
        <div class="main-container">
            <!-- 左侧菜单 -->
            <transition name="menu-fade" mode="out-in">
                <el-menu v-if="activeMenuConfig" :key="activeTopNav" class="aside-menu"
                    :default-active="defaultActiveIndex" background-color="#ffffff" text-color="#606266"
                    active-text-color="#ffffff" :collapse="isCollapse" router unique-opened>
                    <!-- 动态生成菜单项 -->
                    <template v-for="item in activeMenuConfig.items" :key="item.index">
                        <!-- 有子菜单的情况 -->
                        <el-sub-menu v-if="item.children && item.children.length" :index="item.index">
                            <template #title>
                                <el-icon>
                                    <component :is="item.icon" />
                                </el-icon>
                                <span>{{ item.title }}</span>
                            </template>
                            <el-menu-item v-for="child in item.children" :key="child.index" :index="child.index">
                                <span style="font-size: 0.85rem;">{{ child.title }}</span>
                            </el-menu-item>
                        </el-sub-menu>

                        <!-- 没有子菜单的情况 -->
                        <el-menu-item v-else :index="item.index">
                            <el-icon>
                                <component :is="item.icon" />
                            </el-icon>
                            <span>{{ item.title }}</span>
                        </el-menu-item>
                    </template>
                </el-menu>
            </transition>
        </div>
    </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useNavigationStore } from '@/store/index'
import { storeToRefs } from 'pinia'
import menuConfig from "@/assets/menuConfig"

const router = useRouter()
const route = useRoute()
const navigationStore = useNavigationStore()
const { activeTopNav } = storeToRefs(navigationStore)
const isCollapse = ref(false)
const defaultActiveIndex = ref('')

// 根据当前激活的顶部导航获取对应的菜单配置
const activeMenuConfig = computed(() => {
    return menuConfig.find(config => config.activeTopNav === activeTopNav.value)
})

// 获取当前菜单组的第一个有效路由
const getFirstValidRoute = (items) => {
    for (const item of items) {
        if (item.children && item.children.length) {
            return item.children[0].index
        } else if (!item.children) {
            return item.index
        }
    }
    return ''
}

// 从本地存储中获取上次激活的菜单路径
const getStoredActiveRoute = () => {
    return localStorage.getItem('activeRoute') || ''
}

// 保存当前激活的菜单路径到本地存储
const saveActiveRoute = (routePath) => {
    localStorage.setItem('activeRoute', routePath)
}

// 监听菜单配置变化，设置默认选中项
watch(activeMenuConfig, (newConfig) => {
    if (newConfig && newConfig.items.length > 0) {
        let firstRoute = getStoredActiveRoute()
        const currentRouteMatched = newConfig.items.some(item => {
            return item.index === firstRoute ||
                (item.children && item.children.some(child => child.index === firstRoute))
        })

        if (!currentRouteMatched) {
            firstRoute = getFirstValidRoute(newConfig.items)
        }

        defaultActiveIndex.value = firstRoute

        // 如果当前路由不在当前菜单组中，则跳转到第一个菜单项
        const currentRouteMatchedInMenu = newConfig.items.some(item => {
            return item.index === route.path ||
                (item.children && item.children.some(child => child.index === route.path))
        })

        if (!currentRouteMatchedInMenu && firstRoute) {
            router.push(firstRoute)
        }

        saveActiveRoute(firstRoute)
    }
}, { immediate: true })

// 监听路由变化，更新本地存储中的激活路径
watch(route, (newRoute) => {
    saveActiveRoute(newRoute.path)
})
</script>

<style scoped lang="less">
html {
    font-size: 16px;
}

.app-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
}

.main-container {
    padding-top: 0.65rem;
    display: flex;
    flex: 1;
    overflow: hidden;
}

.aside-menu {
    height: 100%;
    width: 13.75rem;
    border-right: 0.0625rem solid #e6e6e6;
    transition: all 0.3s ease;

    &:not(.el-menu--collapse) {
        width: 13.75rem;
    }

    &.el-menu--collapse {
        width: 4rem;

        :deep(.el-sub-menu__title span),
        :deep(.el-menu-item span) {
            display: none;
        }
    }

    :deep(.el-menu-item),
    :deep(.el-sub-menu__title) {
        height: 3.125rem;
        line-height: 3.125rem;
        padding: 0 1rem;
        transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);

        .el-icon {
            font-size: 1.125rem;
            margin-right: 0.5rem;
        }

        span {
            font-size: 1rem;
        }
    }

    :deep(.el-menu-item.is-active) {
        background-color: #1a75ff !important;
        color: #fff !important;
        border-radius: 0.25rem;
        margin: 0 0.5rem;
        width: calc(100% - 1rem);
        box-shadow: 0 0.125rem 0.5rem rgba(67, 97, 238, 0.2);

        .el-icon {
            color: #fff !important;
        }
    }

    :deep(.el-menu-item:hover) {
        background-color: #f8f9fe !important;
        color: #1a75ff !important;
        transform: translateX(0.25rem);

        .el-icon {
            color: #1a75ff !important;
        }
    }

    :deep(.el-sub-menu__title:hover) {
        background-color: #f8f9fe !important;
        color: #1a75ff !important;

        .el-icon {
            color: #1a75ff !important;
        }
    }

    :deep(.el-menu--inline .el-menu-item:hover) {
        background-color: #f0f2ff !important;
        transform: none;
        padding-left: 1.25rem !important;
    }
}

/* 动画效果 */
.menu-fade-enter-active,
.menu-fade-leave-active {
    transition: all 0.3s ease;
}

.menu-fade-enter-from {
    opacity: 0;
    transform: translateX(-20px);
}

.menu-fade-leave-to {
    opacity: 0;
    transform: translateX(20px);
}

.main-content {
    flex: 1;
    padding: 1.5rem;
    overflow-y: auto;
    background-color: #f8f9fa;
}
</style>